/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-08-24     yangjie      the first version
 */
/*
* 程序清单：定时器例程
*
* 这个例程会创建两个动态定时器，一个是单次定时，一个是周期性定时
* 并让周期定时器运行一段时间后停止运行
*/
#include "hal_timer.h"
#include "hal_esp8266.h"
#include "hal_common.h"
#include "rtthread.h"

/* 定时器的控制块 */
static rt_timer_t timer1;
static int cnt = 0;

// [09:58:15.184]收←◆timer --------> data 0,CONNECT

// [09:58:48.611]收←◆timer --------> data
// +IPD,0,5:hell0

// [09:58:52.816]收←◆timer --------> data 0,CLOSED

/* 定时器1超时函数 */
static void timeout1(void *parameter)
{
    int sock_id = -1;
    char buff[128] = { 0x00 };
    int len = 0;
    sys_event_e event = STA_EVENT_MAX;
    
//	rt_kprintf("timer's cnt is %d, FrameLength is %d\r\n", cnt, Esp8266_Frame_Record.FramLength);
    if (1 == Esp8266_Frame_Record.InfBit.FramStartFlag)
    {

    	if (cnt == Esp8266_Frame_Record.FramLength && cnt != 0)
    	{
    		cnt = 0;
            Esp8266_Frame_Record .Data_RX_BUF [ Esp8266_Frame_Record.FramLength ]  = 0x00;
//        	rt_kprintf("timer --------> data %s\r\n", Esp8266_Frame_Record.Data_RX_BUF);

			// http data include both "CONNECT" and "IPD"
			if (rt_strstr(Esp8266_Frame_Record.Data_RX_BUF, "CONNECT") && rt_strstr(Esp8266_Frame_Record.Data_RX_BUF, "+IPD"))
			{
                rt_memset(hal_sys_contex_get()->data_buf, 0x00, SYS_CTX_UART_RECV_SIZE);
                sscanf(Esp8266_Frame_Record.Data_RX_BUF, "%*[^+]+IPD,%d,%d:%[^\r]", &sock_id, &len, hal_sys_contex_get()->data_buf);
                event = STA_DATA_ARRIVED;
			}
        	else if (rt_strstr(Esp8266_Frame_Record.Data_RX_BUF, "CONNECT"))
            {
            	sscanf(Esp8266_Frame_Record.Data_RX_BUF, "%d,%s", &sock_id, buff);
                event = STA_CONNECTED;
            }else if (rt_strstr(Esp8266_Frame_Record.Data_RX_BUF, "CLOSED"))
            {
            	sscanf(Esp8266_Frame_Record.Data_RX_BUF, "%d,%s", &sock_id, buff);
                event = STA_CLOSED;
            }else if (rt_strstr(Esp8266_Frame_Record.Data_RX_BUF, "+IPD"))
            {
                rt_memset(hal_sys_contex_get()->data_buf, 0x00, SYS_CTX_UART_RECV_SIZE);
                sscanf(Esp8266_Frame_Record.Data_RX_BUF, "%*[^+]+IPD,%d,%d:%[^\r]", &sock_id, &len, hal_sys_contex_get()->data_buf);
                event = STA_DATA_ARRIVED;
//                rt_kprintf("parsed +IPD :%s\r\n", hal_sys_contex_get()->data_buf);

            }
            // call sys_status_cb
            if (hal_sys_contex_get()->sys_status_cb)
            {
                hal_sys_contex_get()->sys_status_cb(sock_id, event);
            }
            
    		Esp8266_Frame_Record.InfBit.FramFinishFlag = 1;
    		Esp8266_Frame_Record.InfBit.FramStartFlag = 0;
    	}else
    	{
    		cnt = Esp8266_Frame_Record.FramLength;
    	}
    }else
    {
    	cnt = 0;
        Esp8266_Frame_Record.FramLength = 0;
    }
}

int hal_timer_init(void)
{
    /* 创建定时器1  周期定时器 */
    timer1 = rt_timer_create("timer1", timeout1,
                             RT_NULL, 50, RT_TIMER_FLAG_PERIODIC);
    /* 启动定时器1 */
    if (timer1 != RT_NULL)
        rt_timer_start(timer1);

    return 0;
}

